﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.BusinessLogicLayer;
using System.Data;
using VideoClassLibrary.Model;

public partial class TrainClassManagement_QuantityAddTrainingClass : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetAutoCompleteContextKey();
        if (!IsPostBack)
        {
            GetSchoolNameById();
        }
    }

    private void SetAutoCompleteContextKey()
    {
        for (int i = 1; i <= 10; i++)
        {
            AjaxControlToolkit.AutoCompleteExtender AceTrainingClass = (AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AceTrainingClass" + i);
            AceTrainingClass.ContextKey = this.SchoolID.ToString();
        }
    }
    //获取当前所属学校
    private void GetSchoolNameById()
    {
        TeacherModule ta = new TeacherModule();
        string schoolName = ta.GetSchNameByID(this.SchoolID);
        for (int i = 1; i <= 10; i++)
        {
            TextBox txtSchool = (TextBox)this.FindControl("txtSchool" + i);
            txtSchool.Text = schoolName;
            txtSchool.Enabled = false;
        }

    }
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string[] GetTrainingClass(string prefixText, int count, string contextKey)
    {
        try
        {
            string[] result = null;
            TrainClassModule trainingModule = new TrainClassModule();
            DataSet ds = trainingModule.GetTrainclassListByWhere(int.Parse(contextKey), "  and LOWER(VTC_name) like '" + prefixText.ToLower() + "%'");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                result = new string[ds.Tables[0].Rows.Count];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    result[i] = "{'First':'" + dr["VTC_name"].ToString() + "','Second':" + dr["ID"].ToString() + "}";

                }
            }
            return result;
        }
        catch
        {
            // TODO: logging...
            return null;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TrainClassModule trainingModule = new TrainClassModule();
        for (int i = 1; i <= 10; i++)
        {
            int scID = this.CurrentUser.SchoolID;

            TextBox txBeginDate = (TextBox)this.FindControl("txtStarDate" + i);
            TextBox txEndDate = (TextBox)this.FindControl("txtEndDate" + i);
            TextBox txTrainClassName = (TextBox)this.FindControl("txtTrainClass" + i);
            TextBox txDesc = (TextBox)this.FindControl("txtDesc" + i);
            TextBox txNum = (TextBox)this.FindControl("txtTrainClassNum" + i);
            TextBox txCount = (TextBox)this.FindControl("txtMaxCount" + i);


            Label lblMsg = (Label)this.FindControl("lblMsg" + i);

            if ( !string.IsNullOrEmpty(txBeginDate.Text.Trim()) && !string.IsNullOrEmpty(txEndDate.Text.Trim())
                 && !string.IsNullOrEmpty(txTrainClassName.Text.Trim()))
            {
                V_TrainClass trainingModel = new V_TrainClass();
                trainingModel.Create_time = DateTime.Now;
                trainingModel.SchoolID = this.SchoolID;
                trainingModel.update_time = DateTime.Now;
                trainingModel.VTC_name = txTrainClassName.Text;
                trainingModel.VTC_StartDate = DateTime.Parse(txBeginDate.Text);
                trainingModel.VTC_EndDate = DateTime.Parse(txEndDate.Text);
                trainingModel.VTC_Number = txNum.Text;
                trainingModel.MaxUserCount =Convert.ToInt32(txCount.Text);

                int  result = trainingModule.AddTrainclass(trainingModel);
                lblMsg.Text = result > 0? "成功" : "失败";
            }
            else
            {
                if (!string.IsNullOrEmpty(txTrainClassName.Text.Trim()))
                {
                    lblMsg.Text = "信息不完整！";
                }
                
            }
        }
    }
    protected void btnRedirectList_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewTrainingClassList.aspx");
    }
}
